from page.net_project_page import NetProjectPage
from common.page import IOS, XPATH
from selenium.common.exceptions import NoSuchElementException
import time as t


num_dict = {1: "一", 2: "二", 3: "三", 4: "四", 5: "五", 6: "六",
            7: "七", 8: "八", 9: "九", 10: "十", 11: "十一"}


class xbActPage(NetProjectPage):
    # 列表页
    not_join = (IOS, "type == 'XCUIElementTypeStaticText' AND label == '未参与'")
    # 活动详情页
    act_info = (IOS, "type == 'XCUIElementTypeStaticText' AND label == '查看详情'")
    # 步骤参与页
    # 步骤3
    download_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == '点击下载'")
    # 步骤4
    photo_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == '椭圆2'")
    album_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == '拍照上传'")
    # (IOS, "type == 'XCUIElementTypeButton' AND label == '图库'")    XCUIElementTypeImage
    # image_cell = (IOS, "type == 'XCUIElementTypeCell'")
    # image_confirm = (IOS, "type == 'XCUIElementTypeButton' AND label == '完成'")
    image_name = (IOS, "type == 'XCUIElementTypeTextField' AND value == 'Img'")
    name_confirm = (IOS, "type == 'XCUIElementTypeButton' AND label == '确定'")
    # 步骤5
    proposition_btn = (IOS, "type == 'XCUIElementTypeStaticText' AND label == '正方'")
    opposition_btn = (IOS, "type == 'XCUIElementTypeStaticText' AND label == '反方'")
    # 步骤6
    video_cell = (IOS, "type == 'XCUIElementTypeCell'")
    # 步骤7
    video_title = (IOS, "type == 'XCUIElementTypeStaticText' AND label CONTAINS '护送蛋宝宝'")
    # 步骤10
    option_1 = (IOS, "type == 'XCUIElementTypeStaticText' AND label == '选项一'")
    push_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == '投票'")
    # 步骤11
    grade_A = (IOS, "type == 'XCUIElementTypeButton' AND label == '很满意'")
    grade_B = (IOS, "type == 'XCUIElementTypeButton' AND label == '满意'")
    grade_C = (IOS, "type == 'XCUIElementTypeButton' AND label == '一般'")
    grade_D = (IOS, "type == 'XCUIElementTypeButton' AND label == '很满意'")
    text_area_1 = (XPATH, "//XCUIElementTypeTable/XCUIElementTypeTextView[1]")
    text_area_2 = (XPATH, "//XCUIElementTypeTable/XCUIElementTypeTextView[2]")
    # text_area_2 = (IOS,"type == 'XCUIElementTypeStaticText' AND label == '对活动的意见或建议...'")
    hidden_name = (IOS, "type == 'XCUIElementTypeButton' AND label == '匿名'")
    submit_suggestion = (IOS, "type == 'XCUIElementTypeButton' AND label == '提交评价'")

    def enter_step(self, step):  # 进入步骤的方法
        for i in range(5):
            try:
                self.find_element(IOS, "type == 'XCUIElementTypeStaticText' "
                                       "AND name CONTAINS '步骤%s'" % num_dict[step]).click()
            except NoSuchElementException:
                t.sleep(0.5)
                self.swipe_dr('up')
            else:
                break

    def area_input(self,area_title, text=''):  # 输入评论的通用方法    area_title,
        # self.find_element(IOS, "type == 'XCUIElementTypeTextField' AND value CONTAINS '%s'" % area_title).click()
        # self.find_element(XPATH, " //XCUIElementTypeTextField[contains(@value, '%s')]'%s'" %area_title).click()
        if text:
            self.find_element(XPATH, "//XCUIElementTypeStaticText[contains(@name, '%s')]"
                                     "/../XCUIElementTypeTextView" % text).send_keys("这是%s的内容。。。。不要因为命运的怪诞而俯首听命于它，等你年老的时候，回首往事，你获得的就越丰硕。在你彻底绝望的时候，别忘了自己拥有一半的命运；用你自己的一半去获取上帝手中的一半。 这就是命运的一生；这就是一生的命运。" % area_title)
        else:
            self.find_element(XPATH, "//XCUIElementTypeStaticText[contains(@name, '请输入')]"
                                     "/../XCUIElementTypeTextView").send_keys("这是%s的内容。。。。相反，那种不知敬畏的人是从不在人格上反省自己的。如果说�知耻近乎勇�，那么，这种人因为不知耻便显出一种卑怯的放肆；获取再多的名声，也不知什么是光荣。不相信神圣 的人，必被世上一切神圣的事物所抛弃。" % area_title)
        self.find_element(IOS, "type == 'XCUIElementTypeButton' AND label == '发布'").click()

    def join_act(self):     #6
        # 进入活动
        # self.swipe_dr('up')
        self.find_element(*self.not_join).click()
        self.find_element(*self.act_info).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤1
        self.enter_step(1)
        # self.area_input("写评论")
        # self.find_text_view(string="写评论",contains_flag=1).click()
        # self.find_button(string="写评论",contains_flag=1).click()
        self.swipe(30,627,30,627)   #只有像素可以定位到输入框
        # self.find_xpath('//*[@name="研学通"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[2]/XCUIElementTypeTextField[1]').click()
        self.area_input("写评论")
        t.sleep(2)
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤2
        self.enter_step(2)
        self.swipe(30, 627, 30, 627)
        self.area_input("提问")
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤3
        self.enter_step(3)
        self.find_element(*self.download_btn).click()
        t.sleep(3)
        self.swipe(30, 627, 30, 627)
        self.area_input("写评论", "写评论")
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤4
        self.enter_step(4)
        self.find_element(*self.photo_btn).click()
        self.find_element(*self.album_btn).click()
        self.find_button("拍照").click()
        t.sleep(1)
        self.find_button("使用照片").click()
        # self.find_elements(*self.image_cell)[-1].click()
        # self.find_element(*self.image_confirm).click()
        self.find_element(*self.image_name).send_keys("图片名称")
        self.find_element(*self.name_confirm).click()
        t.sleep(0.5)
        self.find_button("点击下载").click()
        self.swipe(55, 365, 55, 365)
        t.sleep(1)
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤5
        self.enter_step(5)
        self.find_element(*self.proposition_btn).click()
        self.swipe(30, 633, 30, 633)
        self.area_input("写观点")
        self.find_element(*self.opposition_btn).click()
        self.swipe(30, 633, 30, 633)
        self.area_input("写观点")
        t.sleep(1)
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤6
        self.enter_step(6)
        self.swipe_dr('down')
        t.sleep(1)
        self.find_elements(*self.video_cell)[1].click()
        t.sleep(2)
        self.find_button(string="返回", contains_flag=1).click()
        # self.find_button(string="返回", contains_flag=1).click()
        self.swipe(30, 627, 30, 627)
        try:
            self.area_input('写观点')
        except NoSuchElementException:
            print("已退回到列表页")
        else:
            self.find_button(string="返回", contains_flag=1).click()
        # 步骤7
        self.swipe_dr('up')
        self.enter_step(7)
        self.swipe(30,600,30,550)
        self.find_element(*self.video_title).click()
        t.sleep(15)
        self.swipe(30, 633, 30, 633)
        self.area_input("写评论")
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤8
        self.enter_step(8)
        self.find_element(*self.photo_btn).click()
        self.find_element(*self.album_btn).click()
        self.find_button("拍照").click()
        t.sleep(1)
        self.find_button("使用照片").click()
        # self.find_elements(*self.image_cell)[-1].click()
        # self.find_element(*self.image_confirm).click()
        self.find_element(*self.image_name).send_keys("图片名称")
        self.find_element(*self.name_confirm).click()
        self.find_button("点击下载").click()
        self.swipe(55,365,55,365)
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤9
        self.enter_step(9)
        self.find_element(*self.download_btn).click()
        t.sleep(2)
        self.swipe(30, 627, 30, 627)
        self.area_input("写评论", "写评论")
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤10
        self.enter_step(10)
        self.find_element(*self.option_1).click()
        self.find_element(*self.push_btn).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤11
        self.enter_step(11)
        self.find_element(*self.grade_B).click()
        self.find_element(*self.text_area_1).send_keys("这是收货。。。。。相反，那种不知敬畏的人是从不在人格上反省自己的。如果说�知耻近乎勇�，那么，这种人因为不知耻便显出一种卑怯的放肆。只要不受惩罚，他敢于践踏任何美好的东西，包括爱情、友谊、荣誉，而且内心没有丝毫不安。这样的人尽管有再多的艳遇，也没有能力真正爱一回；结交再多的哥们，也体味不了友谊的纯正；获取再多的名声，也不知什么是光荣。不相信神圣 的人，必被世上一切神圣的事物所抛弃。")
        self.find_xpath('//*[@name="步骤十一"]').click()
        self.find_element(*self.text_area_2).send_keys("这是建议。。。。。相反，那种不知敬畏的人是从不在人格上反省自己的。如果说�知耻近乎勇�，那么，这种人因为不知耻便显出一种卑怯的放肆。只要不受惩罚，他敢于践踏任何美好的东西，包括爱情、友谊、荣誉，而且内心没有丝毫不安。这样的人尽管有再多的艳遇，也没有能力真正爱一回；结交再多的哥们，也体味不了友谊的纯正；获取再多的名声，也不知什么是光荣。不相信神圣 的人，必被世上一切神圣的事物所抛弃。")
        self.swipe(155,31,155,31)
        self.find_element(*self.hidden_name).click()
        self.find_element(*self.submit_suggestion).click()
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()


    def join_acts(self):  #6s
        # 进入活动
        # self.swipe_dr('up')
        self.find_element(*self.not_join).click()
        self.find_element(*self.act_info).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤1
        self.enter_step(1)
        # self.area_input("写评论")
        # self.find_text_view(string="写评论",contains_flag=1).click()
        # self.find_button(string="写评论",contains_flag=1).click()
        self.swipe(20,693,20,693)    #6s 写评论（像素）
        # self.find_xpath('//*[@name="研学通"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[2]/XCUIElementTypeTextField[1]').click()
        self.area_input("写评论")
        t.sleep(1)
        self.swipe(244,391,244,391)    # 二级评论
        t.sleep(0.5)
        self.swipe(20, 693, 20, 693)
        # self.find_static_text(string="请输入...",contains_flag=1).click()
        self.area_input("写评论")
        t.sleep(2)
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤2
        self.enter_step(2)
        self.swipe(20,693,20,693)
        self.area_input("提问")
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤3
        self.enter_step(3)
        self.find_element(*self.download_btn).click()
        t.sleep(3)
        self.swipe(20,693,20,693)
        self.area_input("写评论", "写评论")
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤4
        self.enter_step(4)
        self.find_element(*self.photo_btn).click()
        self.find_element(*self.album_btn).click()
        self.find_button("拍照").click()
        t.sleep(1)
        self.find_button("使用照片").click()
        # self.find_elements(*self.image_cell)[-1].click()
        # self.find_element(*self.image_confirm).click()
        self.find_element(*self.image_name).send_keys("图片名称")
        self.find_element(*self.name_confirm).click()
        t.sleep(0.5)
        self.find_button("点击下载").click()
        self.swipe(40,393,40,393)
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤5
        self.enter_step(5)
        self.find_element(*self.proposition_btn).click()
        self.swipe(20,699,20,699)
        self.area_input("写观点")
        self.find_element(*self.opposition_btn).click()
        self.swipe(20,699,20,699)
        self.area_input("写观点")
        t.sleep(1)
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤6
        self.enter_step(6)
        self.swipe_dr('down')
        t.sleep(1)
        self.find_elements(*self.video_cell)[1].click()
        t.sleep(2)
        self.find_button(string="返回", contains_flag=1).click()
        # self.find_button(string="返回", contains_flag=1).click()
        self.swipe(20,693,20,693)
        try:
            self.area_input('写观点')
        except NoSuchElementException:
            print("已退回到列表页")
        else:
            self.find_button(string="返回", contains_flag=1).click()
        # 步骤7
        self.swipe_dr('up')
        self.enter_step(7)
        self.swipe(30,600,30,550)   #滑动
        self.find_element(*self.video_title).click()
        t.sleep(15)
        self.swipe(100, 693, 100, 693)   #点击评论按钮
        self.area_input("写评论")
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤8
        self.enter_step(8)
        self.find_element(*self.photo_btn).click()
        self.find_element(*self.album_btn).click()
        self.find_button("拍照").click()
        t.sleep(1)
        self.find_button("使用照片").click()
        # self.find_elements(*self.image_cell)[-1].click()
        # self.find_element(*self.image_confirm).click()
        self.find_element(*self.image_name).send_keys("图片名称")
        self.find_element(*self.name_confirm).click()
        self.find_button("点击下载").click()
        self.swipe(100,390,100,390)
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤9
        self.enter_step(9)
        self.find_element(*self.download_btn).click()
        t.sleep(2)
        self.swipe(20,693,20,693)
        self.area_input("写评论", "写评论")
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤10
        self.enter_step(10)
        self.find_element(*self.option_1).click()
        self.find_element(*self.push_btn).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤11
        self.enter_step(11)
        self.find_element(*self.grade_B).click()
        self.find_element(*self.text_area_1).send_keys("这是收货。。。。一个人可以不信神，但不可以不相信神圣。是否相信上帝、佛、真主或别的什么主宰宇宙的神秘力量，往往取决于个人所隶属的民族传统、文化背景和个人的特殊经历，甚至取决于个人的某种神秘体验，这是勉强不得的。一个没有这些宗教信仰的人，仍然可能是一个善良的人。")
        self.find_xpath('//*[@name="步骤十一"]').click()
        self.find_element(*self.text_area_2).send_keys("这是建议。。。。一个人可以不信神，但不可以不相信神圣。是否相信上帝、佛、真主或别的什么主宰宇宙的神秘力量，往往取决于个人所隶属的民族传统、文化背景和个人的特殊经历，甚至取决于个人的某种神秘体验，这是勉强不得的。一个没有这些宗教信仰的人，仍然可能是一个善良的人。")
        # self.swipe(182,31,182,31)
        self.find_static_text(string="步骤11",contains_flag=1).click()
        self.find_element(*self.hidden_name).click()
        self.find_element(*self.submit_suggestion).click()
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="common back light icon", contains_flag=1).click()


    def xin_act(self):  #6s
        # 进入活动
        # self.swipe_dr('up')
        self.find_element(*self.not_join).click()
        self.find_element(*self.act_info).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤1
        self.enter_step(1)
        # self.area_input("写评论")
        # self.find_text_view(string="写评论",contains_flag=1).click()
        # self.find_button(string="写评论",contains_flag=1).click()
        self.swipe(20,693,20,693)    #6s 写评论（像素）
        # self.find_xpath('//*[@name="研学通"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[2]/XCUIElementTypeTextField[1]').click()
        self.area_input("写评论")
        t.sleep(1)
        self.swipe(244,391,244,391)    # 二级评论
        t.sleep(0.5)
        self.swipe(20, 693, 20, 693)
        # self.find_static_text(string="请输入...",contains_flag=1).click()
        self.area_input("写评论")
        t.sleep(2)
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤2
        self.enter_step(2)
        self.swipe(20,693,20,693)
        self.area_input("提问")
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤3
        self.enter_step(3)
        self.find_element(*self.download_btn).click()
        t.sleep(3)
        self.swipe(20,693,20,693)
        self.area_input("写评论", "写评论")
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤4
        self.enter_step(4)
        self.find_element(*self.photo_btn).click()
        self.find_element(*self.album_btn).click()
        self.find_button("拍照").click()
        t.sleep(1)
        self.find_button("使用照片").click()
        # self.find_elements(*self.image_cell)[-1].click()
        # self.find_element(*self.image_confirm).click()
        self.find_element(*self.image_name).send_keys("图片名称")
        self.find_element(*self.name_confirm).click()
        t.sleep(0.5)
        self.find_button("点击下载").click()
        self.swipe(40,393,40,393)
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤5
        self.enter_step(5)
        self.find_element(*self.proposition_btn).click()
        self.swipe(20,699,20,699)
        self.area_input("写观点")
        self.find_element(*self.opposition_btn).click()
        self.swipe(20,699,20,699)
        self.area_input("写观点")
        t.sleep(1)
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤6
        self.enter_step(6)
        self.swipe_dr('down')
        t.sleep(1)
        self.find_elements(*self.video_cell)[1].click()
        t.sleep(2)
        self.find_button(string="返回", contains_flag=1).click()
        # self.find_button(string="返回", contains_flag=1).click()
        self.swipe(20,693,20,693)
        try:
            self.area_input('写观点')
        except NoSuchElementException:
            print("已退回到列表页")
        else:
            self.find_button(string="返回", contains_flag=1).click()
        # 步骤7
        self.swipe_dr('up')
        self.enter_step(7)
        self.swipe(30,600,30,550)   #滑动
        self.find_element(*self.video_title).click()
        t.sleep(15)
        self.swipe(100, 693, 100, 693)   #点击评论按钮
        self.area_input("写评论")
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤8
        self.enter_step(8)
        self.find_element(*self.photo_btn).click()
        self.find_element(*self.album_btn).click()
        self.find_button("拍照").click()
        t.sleep(1)
        self.find_button("使用照片").click()
        # self.find_elements(*self.image_cell)[-1].click()
        # self.find_element(*self.image_confirm).click()
        self.find_element(*self.image_name).send_keys("图片名称")
        self.find_element(*self.name_confirm).click()
        self.find_button("点击下载").click()
        self.swipe(100,390,100,390)
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤9
        self.enter_step(9)
        self.find_element(*self.download_btn).click()
        t.sleep(2)
        self.swipe(20,693,20,693)
        self.area_input("写评论", "写评论")
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤10
        self.enter_step(10)
        self.find_element(*self.option_1).click()
        self.find_element(*self.push_btn).click()
        self.find_button(string="返回", contains_flag=1).click()
        # 步骤11
        self.enter_step(11)
        self.find_element(*self.grade_B).click()
        self.find_element(*self.text_area_1).send_keys("这是收货。。。。一个人可以不信神，但不可以不相信神圣。是否相信上帝、佛、真主或别的什么主宰宇宙的神秘力量，往往取决于个人所隶属的民族传统、文化背景和个人的特殊经历，甚至取决于个人的某种神秘体验，这是勉强不得的。一个没有这些宗教信仰的人，仍然可能是一个善良的人。")
        self.find_xpath('//*[@name="步骤十一"]').click()
        self.find_element(*self.text_area_2).send_keys("这是建议。。。。一个人可以不信神，但不可以不相信神圣。是否相信上帝、佛、真主或别的什么主宰宇宙的神秘力量，往往取决于个人所隶属的民族传统、文化背景和个人的特殊经历，甚至取决于个人的某种神秘体验，这是勉强不得的。一个没有这些宗教信仰的人，仍然可能是一个善良的人。")
        # self.swipe(182,31,182,31)
        self.find_element(XPATH,'//*[@name="步骤11"]/XCUIElementTypeOther[1]').click()
        self.find_element(*self.hidden_name).click()
        self.find_element(*self.submit_suggestion).click()
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="common back light icon", contains_flag=1).click()

if __name__ == "__main__":
    test = xbActPage()
    test.enter_project()
    test.enter_tools('活动')
    test.join_act()
